{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Lists and Loops [demonstration]\n",
    "The code below shows some ways of using python **lists** (known as **arrays** in many languages).  Note how these lists are \"sliced\" from index i to j using `my_list[i:j]` notation."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# How to print a list structure\n",
    "my_list = [1, 2, 3, \"a\", \"b\", \"c\"]\n",
    "print(\"my_list is:\", my_list)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Prints each element of a list individually\n",
    "print(\"Looping through a list...\")\n",
    "for item in my_list:\n",
    "    print(\"item is\", item)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Prints the number of elements in a list\n",
    "print(\"The len function is important!\")\n",
    "num_elements = len(my_list)\n",
    "print(\"my_list has\", num_elements, \"elements\")\n",
    "\n",
    "# Could also be done with out the intermediate variable\n",
    "print(\"The len function is important!\")\n",
    "print(\"my_list has\", len(my_list), \"elements\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Using range to loop through a list by index\n",
    "print(\"A less great way to loop through a list...\")\n",
    "for index in range(len(my_list)):\n",
    "    item = my_list[index] # accessing an element in a list!\n",
    "    print(\"item is\", item)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Looping through a partial list\n",
    "print(\"Slicing a list from beginning...\")\n",
    "for item in my_list[:3]:\n",
    "    print(\"item is\", item)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Looping through a partial list again\n",
    "print(\"Slicing a list to the end...\")\n",
    "for item in my_list[3:]:\n",
    "    print(\"item is\", item)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Looping through a partial list again\n",
    "print(\"Slicing a list in the middle...\")\n",
    "for item in my_list[2:4]:\n",
    "    print(\"item is\", item)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "print(\"Enumerating a list...\")\n",
    "for i, item in enumerate(my_list):\n",
    "    print(\"item number\", i, \"is\", item)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "print(\"Another way to enumerate using a list 'method'...\")\n",
    "for item in my_list:\n",
    "    index = my_list.index(item)\n",
    "    print(\"item\", item, \"has index\", index)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
